SG Window
Handling Massages

©1998 by Stinga

Overview      Reference     How To...     FAQ

One of the most important features of the SG Window is to enable processing of the window messages from the Visual Basic programs. To intercept window messages, SG Window subclasses a window and inserts custom window procedure into the window's message processing chain.
It is possible to subclass a window using VB's AddressOf operator, but it has many drawbacks:

SG Window gives you two methods for message handling:

Standard Events

To receive window message events, follow these simple steps:

  1. Declare SG Window object in the form or class module. Use WithEvents keyword to connect to the SG Window events.
  2. Create SG Window object
  3. Enable messages
  4. Write message event handler code
' Declare SGWindow object
Private WithEvents mWnd As SGWindow.Window

Private Sub Form_OnLoad()
   ' Crete object
   Set mWnd = New SGWindow.Window

   ' Enable messages
   mWnd.EnableMessage wm_MOUSEWHEEL
   mWnd.hWnd = Me.hWnd
   mWnd.Hooked = True
End Sub

Private Sub mWnd_Message(ByVal msg As Long, ByVal wParam As Long, _
                    ByVal lParam As Long, ByRef result As Long)
   ' Write some useful code here ...

   ' ... and/or call default window procedure
   result = mWnd.CallWindowProc(msg, wParam, lParam)
End Sub
Message Callback interface

SG Window defines message callback interface which you can implement in the form or class module. When you attach your implementation of this interface to the SG Window object, interface's Message method will be called whenever new message arrives. Message callbacks mechanism is faster than event handling and you will use it when speed is important.

Following is an example of a callback interface implemented in the class module:

' Declare message callback interface
Implements IsgMessageSink

' Declare SGWindow object
Private mWnd As SGWindow.Window

Private Sub Class_Initialize()
   ' Crete object
   Set mWnd = New SGWindow.Window

   ' Enable messages
   mWnd.EnableMessage wm_MOUSEWHEEL

   ' Attach callback interface
   mWnd.SetMessageCallback Me
End Sub

Private Sub Class_Terminate()
   ' Destroy object
   Set mWnd = Nothing
End Sub

Private Sub IsgMessagesink_Message(ByVal msg As Long, ByVal wParam As Long, _
                    ByVal lParam As Long, ByRef result As Long)
   ' Write some useful code here ...
   Debug.Print CStr(LowWord(lParam))

   ' ... and/or call default window procedure
   result = mWnd.CallWindowProc(msg, wParam, lParam)
End Sub

' Attach this class to the window
Public Property Let hWnd(hwnd as long)
   mWnd.hWnd = hwnd
   If hwnd <> 0 then
      mWnd.Hooked = True
   Else
      mWnd.Hooked = False
   End If
End Property
Remarks

If message is not handled by the message handler, you must invoke CallWindowProc method and let default window procedure handle message. How and when you are going to call it depends on what you want to accomplish. In some cases you will handle message entirely in the VB code and you don't need to call default window procedure at all.

SG Window Wizard

To simplify use of the SG Window component, we have created SG Window Wizard; a VB add-in that inserts all code required to intercept messages in your VB code. You can find wizard's source code in the samples subfolder of the SG Window installation folder.